package com.x52im.rainbowchat.http.dao;

import com.eva.epc.common.util.CommonUtils;
import com.eva.framework.dbpool.DBDepend;
import com.eva.framework.dbpool.DBShell;
import com.x52im.rainbowchat.http.logic.dto.RedEnvelopeDTO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.stream.Collectors;

/**
 * 钱、账户相关的数据操作
 */
public class MoneyDAO {
    DBShell db = new DBShell();

    /**
     * 插入账户
     *
     * @param uid
     * @return
     * @throws Exception
     */
    public boolean insertAccount(String uid) throws Exception {
        Vector sqls = new Vector();
        Vector values = new Vector();
        sqls.add("INSERT INTO missu_account(user_uid, state,total,create_date) VALUES(?,?,?," + DBDepend.getDefaultDatetimeFunc() + ")");
        values.add(new Object[]{uid, "0", "0"});
        return db.update(sqls, values, true);
    }

    /**
     * 查询账户余额
     *
     * @param uid
     * @return
     */
    public int queryAccountTotal(String uid) throws Exception {
        String total = db.querySingleItem("total", "missu_account", "user_uid=" + uid);
        return Integer.parseInt(total);
    }

    /**
     * 查询单个红包
     *
     * @param idf
     * @return
     * @throws Exception
     */
    public RedEnvelopeDTO queryRedEnvelop(String idf) throws Exception {
        String sql = "select b.nickname, b.user_avatar_file_name, a.id, a.parent_id, a.title, a.order_no ,a.user_uid ,a.total_amount ," +
                "a.state ,a.create_date ,a.state_date ,a.memo ,a.sub_number ,a.sub_money, a.g_id, a.p_id " +
                "from missu_red_envelope a left join missu_users b on a.user_uid =b.user_uid " +
                "where 1=1 and id=?";
        RedEnvelopeDTO envelope = null;
        Vector<Vector> datas = db.queryData(sql, new Object[]{idf});
        for (Vector env : datas) {
            int i = 0;
            String nickname = (String) env.get(i++);
            String user_avatar_file_name = (String) env.get(i++);
            String id = (String) env.get(i++);
            String parent_id = (String) env.get(i++);
            String title = (String) env.get(i++);
            String order_no = (String) env.get(i++);
            String user_uid = (String) env.get(i++);
            String total_amount = (String) env.get(i++);
            String state = (String) env.get(i++);
            String create_date = (String) env.get(i++);
            String state_date = (String) env.get(i++);
            String memo = (String) env.get(i++);
            String sub_number = (String) env.get(i++);
            String sub_money = (String) env.get(i++);
            String g_id = (String) env.get(i++);
            String p_id = (String) env.get(i++);

            envelope = new RedEnvelopeDTO();
            envelope.setNickName(nickname);
            envelope.setAvatar(user_avatar_file_name);
            envelope.setId(id);
            envelope.setParentId(parent_id);
            envelope.setTitle(title);
            envelope.setOrderNo(order_no);
            envelope.setUserUid(user_uid);
            envelope.setTotalAmount(total_amount);
            envelope.setState(state);
            envelope.setCreateDate(create_date);
            envelope.setStateDate(state_date);
            envelope.setMemo(memo);
            envelope.setSubNumber(sub_number);
            envelope.setSubMoney(sub_money);
            envelope.setgId(g_id);
            envelope.setpId(p_id);
        }
        return envelope;

    }

    /**
     * 查询红包列表
     *
     * @param redEnvelopeDTO
     * @param needSubs
     * @return
     */
    public List<RedEnvelopeDTO> queryRedEnvelopsRoot(RedEnvelopeDTO redEnvelopeDTO, boolean needSubs) throws Exception {
        List<RedEnvelopeDTO> envelopes = new ArrayList<>();
        Map<String, RedEnvelopeDTO> idMap = new HashMap();
        String sqls = "select b.nickname, b.user_avatar_file_name, a.id, a.parent_id, a.title, a.order_no ,a.user_uid ,a.total_amount ," +
                "a.state ,a.create_date ,a.state_date ,a.memo ,a.sub_number ,a.sub_money, a.g_id, a.p_id " +
                "from missu_red_envelope a left join missu_users b on a.user_uid =b.user_uid " +
                "where 1=1 ";
        List args = new ArrayList();
        if (!CommonUtils.isStringEmpty(redEnvelopeDTO.getId())) {

            if (needSubs) {
                sqls += "and a.id =? or a.parent_id =? order by a.state_date desc ";
                args.add(redEnvelopeDTO.getId());
                args.add(redEnvelopeDTO.getId());
            } else {
                sqls += "and a.id =?";
                args.add(redEnvelopeDTO.getId());
            }

        }
        Object[] objects = args.toArray();

        Vector<Vector> datas = db.queryData(sqls, objects);
        for (Vector env : datas) {
            int i = 0;
            String nickname = (String) env.get(i++);
            String user_avatar_file_name = (String) env.get(i++);
            String id = (String) env.get(i++);
            String parent_id = (String) env.get(i++);
            String title = (String) env.get(i++);
            String order_no = (String) env.get(i++);
            String user_uid = (String) env.get(i++);
            String total_amount = (String) env.get(i++);
            String state = (String) env.get(i++);
            String create_date = (String) env.get(i++);
            String state_date = (String) env.get(i++);
            String memo = (String) env.get(i++);
            String sub_number = (String) env.get(i++);
            String sub_money = (String) env.get(i++);
            String g_id = (String) env.get(i++);
            String p_id = (String) env.get(i++);

            RedEnvelopeDTO envelope = new RedEnvelopeDTO();
            envelope.setNickName(nickname);
            envelope.setAvatar(user_avatar_file_name);
            envelope.setId(id);
            envelope.setParentId(parent_id);
            envelope.setTitle(title);
            envelope.setOrderNo(order_no);
            envelope.setUserUid(user_uid);
            envelope.setTotalAmount(total_amount);
            envelope.setState(state);
            envelope.setCreateDate(create_date);
            envelope.setStateDate(state_date);
            envelope.setMemo(memo);
            envelope.setSubNumber(sub_number);
            envelope.setSubMoney(sub_money);
            envelope.setgId(g_id);
            envelope.setpId(p_id);

            envelopes.add(envelope);

            if (parent_id.equals("-1")) {
                idMap.put(id, envelope);
            }
        }

        List returnList;
        // 查询子红包的情况
        if (idMap.isEmpty()) {
            returnList = envelopes;
        } else {
            for (String id : idMap.keySet()) {
                RedEnvelopeDTO redEnvelopeDTO0 = idMap.get(id);
                for (int i = 0; i < envelopes.size(); i++) {
                    RedEnvelopeDTO redEnvelopeDTO1 = envelopes.get(i);
                    if (redEnvelopeDTO1.getParentId().equals(id)) {
                        redEnvelopeDTO0.getSubRedEnvelopeDTOS().add(redEnvelopeDTO1);
                    }
                }
            }
            returnList = idMap.values().stream().collect(Collectors.toList());
        }

        return returnList;
    }

    /**
     * 查询红包列表
     *
     * @param redEnvelopeDTO
     * @param needSubs
     * @return
     */
    public List<RedEnvelopeDTO> queryRedEnvelops(RedEnvelopeDTO redEnvelopeDTO, boolean needSubs) throws Exception {
        List<RedEnvelopeDTO> envelopes = new ArrayList<>();
        Map<String, RedEnvelopeDTO> idMap = new HashMap();
        String sqls = "select id, parent_id, title,order_no ,user_uid ,total_amount ,state ,create_date ,state_date ,memo ,sub_number ,sub_money " +
                "from missu_red_envelope a " +
                "where 1=1 ";
        List args = new ArrayList();
        if (!CommonUtils.isStringEmpty(redEnvelopeDTO.getUserUid())) {
            sqls += "and a.user_uid =? ";
            args.add(redEnvelopeDTO.getUserUid());
        }
        if (!CommonUtils.isStringEmpty(redEnvelopeDTO.getOrderNo())) {
            sqls += "and a.order_no =? ";
            args.add(redEnvelopeDTO.getOrderNo());
        }
        if (!CommonUtils.isStringEmpty(redEnvelopeDTO.getId())) {

            if (needSubs) {
                sqls += "and a.id =? or a.parent_id =?";
                args.add(redEnvelopeDTO.getId());
                args.add(redEnvelopeDTO.getId());
            } else {
                sqls += "and a.id =?";
                args.add(redEnvelopeDTO.getId());
            }

        }
        if (!CommonUtils.isStringEmpty(redEnvelopeDTO.getState())) {
            sqls += "and a.state =? ";
            args.add(redEnvelopeDTO.getState());
        }
        Object[] objects = args.toArray();

        Vector<Vector> datas = db.queryData(sqls, objects);
        for (Vector env : datas) {
            int i = 0;
            String id = (String) env.get(i++);
            String parent_id = (String) env.get(i++);
            String title = (String) env.get(i++);
            String order_no = (String) env.get(i++);
            String user_uid = (String) env.get(i++);
            String total_amount = (String) env.get(i++);
            String state = (String) env.get(i++);
            String create_date = (String) env.get(i++);
            String state_date = (String) env.get(i++);
            String memo = (String) env.get(i++);
            RedEnvelopeDTO envelope = new RedEnvelopeDTO();
            envelope.setId(id);
            envelope.setParentId(parent_id);
            envelope.setTitle(title);
            envelope.setOrderNo(order_no);
            envelope.setUserUid(user_uid);
            envelope.setTotalAmount(total_amount);
            envelope.setState(state);
            envelope.setCreateDate(create_date);
            envelope.setStateDate(state_date);
            envelope.setMemo(memo);
            envelopes.add(envelope);

            if (parent_id.equals("-1")) {
                idMap.put(id, envelope);
            }
        }

        List returnList = new ArrayList();
        // 查询子红包的情况
        if (idMap.isEmpty()) {
            returnList = envelopes;
        } else {
            for (String id : idMap.keySet()) {
                RedEnvelopeDTO redEnvelopeDTO0 = idMap.get(id);
                for (int i = 0; i < envelopes.size(); i++) {
                    RedEnvelopeDTO redEnvelopeDTO1 = envelopes.get(i);
                    if (redEnvelopeDTO1.getParentId().equals(id)) {
                        redEnvelopeDTO0.getSubRedEnvelopeDTOS().add(redEnvelopeDTO1);
                    }
                }
            }
            returnList = idMap.values().stream().collect(Collectors.toList());
        }

        return returnList;
    }

}
